package com.example.callcenter.rabbitmq;

import com.example.callcenter.config.RabbitMQConfig;
import com.example.callcenter.dto.SmsDTO;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class SmsConsumer {

    @RabbitListener(queues = RabbitMQConfig.SMS_QUEUE)
    public void handleSms(SmsDTO smsDTO, Channel channel, Message message) throws IOException {
        try {
            // 模拟发送短信
            System.out.println("消费者发送短信到：" + smsDTO.getPhone() + "，验证码：" + smsDTO.getCode());

            // 手动ACK
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            // 发送失败，拒绝并进入死信队列
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}